package com.itheima.service.system.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.dao.system.RoleDao;
import com.itheima.domain.system.Role;
import com.itheima.factory.MapperFactory;
import com.itheima.service.system.RoleService;
import com.itheima.utils.UUIDUtil;
import org.apache.ibatis.session.SqlSession;

import java.util.Date;
import java.util.List;
import java.util.Map;

public class RoleServiceImpl implements RoleService {
    @Override
    public PageInfo<Role> queryPage(Integer pageNum, Integer pageSize) throws Exception {
        SqlSession sqlSession = MapperFactory.getSqlSession();
        RoleDao roleDao = sqlSession.getMapper(RoleDao.class);

        PageHelper.startPage(pageNum,pageSize);
        List<Role> list = roleDao.findAll();
        PageInfo<Role> pageInfo = new PageInfo<>(list);
        sqlSession.close();
        return pageInfo;
    }

    @Override
    public List<Role> findAll() throws Exception {
        SqlSession sqlSession = MapperFactory.getSqlSession();
        RoleDao roleDao = sqlSession.getMapper(RoleDao.class);

        List<Role> list = roleDao.findAll();

        sqlSession.close();

        return list;
    }

    @Override
    public Role findById(String roleId) throws Exception {
        SqlSession sqlSession = MapperFactory.getSqlSession();
        RoleDao roleDao = sqlSession.getMapper(RoleDao.class);

        Role role = roleDao.findById(roleId);

        sqlSession.close();
        return role;
    }

    @Override
    public void save(Role role) throws Exception {
        SqlSession sqlSession = MapperFactory.getSqlSession();
        RoleDao roleDao = sqlSession.getMapper(RoleDao.class);

        role.setId(UUIDUtil.getSimpleUUID());
        role.setCreateTime(new Date());
        roleDao.save(role);

        sqlSession.commit();
        sqlSession.close();
    }

    @Override
    public void update(Role role) throws Exception {
        SqlSession sqlSession = MapperFactory.getSqlSession();
        RoleDao roleDao = sqlSession.getMapper(RoleDao.class);

        roleDao.update(role);

        sqlSession.commit();
        sqlSession.close();
    }

    @Override
    public void deleteById(String... roleIds) throws Exception {
        SqlSession sqlSession = MapperFactory.getSqlSession();
        RoleDao roleDao = sqlSession.getMapper(RoleDao.class);

        for (String roleId : roleIds) {
            roleDao.deleteById(roleId);
        }

        sqlSession.commit();
        sqlSession.close();
    }

    @Override
    public void updateAuthor(String roleId, String... moduleIds) throws Exception {
        SqlSession sqlSession = MapperFactory.getSqlSession();
        RoleDao roleDao = sqlSession.getMapper(RoleDao.class);


        /*1.解除角色关联的模块的权限*/
        roleDao.deleteAuthorByRoleId(roleId);

        /*2.重新为角色绑定模块*/
        if(moduleIds!=null && moduleIds.length>0){
            for (String moduleId : moduleIds) {
                roleDao.saveRoleMoudle(roleId,moduleId);
            }
        }

        sqlSession.commit();
        sqlSession.close();

    }

    @Override
    public List<Map> findRolesByUserId(String userId) throws Exception {
        SqlSession sqlSession = MapperFactory.getSqlSession();
        RoleDao roleDao = sqlSession.getMapper(RoleDao.class);
        List<Map> list = roleDao.findRolesByUserId(userId);
        sqlSession.close();
        return list;
    }


}
